package com.blb.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 包子铺 阻塞队列版
 */
public class BaoziShop2 {

    //临界值
    public static final int MAX_COUNT = 100;
    //阻塞队列
    private BlockingQueue<Baozi> baozis = new ArrayBlockingQueue<>(MAX_COUNT);

    /**
     * 做包子
     */
    public void makeBaozi(){
        Baozi baozi = new Baozi(baozis.size());
        try {
            //做包子放缓冲区
            baozis.put(baozi);
            System.out.println("师傅" + Thread.currentThread().getName() + "做了" + baozi);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 卖包子
     */
    public void sellBaozi(){
        try {
            Baozi baozi = baozis.take();
            System.out.println("消费者" + Thread.currentThread().getName() + "吃了" + baozi);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
